package chapter3_array;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * 把0都移动到尾部
 */
public class MoveZeros {
    static void moveZeros(int[] nums) {
        int a = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                if (i != a) {
                    swap(nums, i, a);
                }
                a++;
            }
        }
    }

    static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }


    public static void main(String[] args) {
        int[] arr = new int[]{0, 1, 0, 2, 0, 3, 5, 6, 0, 7, 8, 8, 9, 9};
        moveZeros(arr);
        System.out.println(Arrays.toString(arr));
    }
}